//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package 力扣.题库.周赛.周赛446;

import java.util.Arrays;

public class Q3 {
    public Q3() {
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
        long[] r = resultArray(nums, 3);
        System.out.println(Arrays.toString(r));
    }

    public static long[] resultArray(int[] nums, int k) {
        long[] result = new long[k];
        int n = nums.length;

        for(int i = 1; i <= n; ++i) {
            long sum = 1L;
            int left = 0;

            for(int j = 0; j < n; ++j) {
                sum = sum * (long)nums[j] % (long)k;
                if (j >= i - 1) {
                    int index = (int)((sum % (long)k + (long)k) % (long)k);
                    int var10002 = (int) result[index]++;
                    sum /= (long)nums[left++];
                }
            }
        }

        return result;
    }

    public long[] resultArray2(int[] nums, int k) {
        long[] result = new long[k];
        int n = nums.length;
        if (k == 0) {
            return result;
        } else {
            int[] modNums = new int[n];

            for(int i = 0; i < n; ++i) {
                modNums[i] = nums[i] % k;
            }

            int[][] buffers = new int[2][k];
            int currentBuffer = 0;

            for(int j = 0; j < n; ++j) {
                int mod = modNums[j];
                if (mod == 0) {
                    result[0] += (long)(j + 1);
                    Arrays.fill(buffers[currentBuffer], 0);
                    buffers[currentBuffer][0] = j + 1;
                } else {
                    int nextBuffer = 1 - currentBuffer;
                    Arrays.fill(buffers[nextBuffer], 0);

                    for(int r = 0; r < k; ++r) {
                        if (buffers[currentBuffer][r] != 0) {
                            int newR = r * mod % k;
                            buffers[nextBuffer][newR] += buffers[currentBuffer][r];
                        }
                    }

                    int var10002 = buffers[nextBuffer][mod]++;

                    for(int r = 0; r < k; ++r) {
                        result[r] += (long)buffers[nextBuffer][r];
                    }

                    currentBuffer = nextBuffer;
                }
            }

            return result;
        }
    }
}
